package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.payconfig.EntityDockList;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoDockList;
import com.ocom.payconfig.service.DockListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DockListServiceImpl extends ServiceImpl<IDaoDockList, EntityDockList> implements DockListService {

    @Autowired
    private IDaoDockList iDaoDockList;

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityDockList> getDockList(String dockCode, String dockName, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        QueryWrapper<EntityDockList> queryWrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(dockCode)){
            queryWrapper.like("dock_name",dockName);
        }
        if (StringUtils.isNotEmpty(dockName)){
            queryWrapper.like("dock_code",dockCode);
        }
        queryWrapper.orderByAsc("dock_code");
        return this.list(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDockList selectDockCode(String dockCode) {
        QueryWrapper<EntityDockList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dock_code",dockCode);
        queryWrapper.last(" limit 1");
        return iDaoDockList.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityDockList selectDockName(String dockName,String dockCode) {
        QueryWrapper<EntityDockList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dock_name",dockName);
        queryWrapper.ne("dock_code",dockCode);
        queryWrapper.last(" limit 1");
        return iDaoDockList.selectOne(queryWrapper);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean saveDockList(EntityDockList entityDockList) {
        return this.save(entityDockList);
    }

    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public boolean updateDock(EntityDockList entityDockList) {
        UpdateWrapper<EntityDockList> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("dock_name",entityDockList.getDockName());
        updateWrapper.set("server_mode",entityDockList.getServerMode());
        updateWrapper.set("sign_mode",entityDockList.getSignMode());
        updateWrapper.set("remark",entityDockList.getRemark());
        updateWrapper.eq("dock_code",entityDockList.getDockCode());
        return this.update(updateWrapper);
    }

    @Override
    public EntityDockList selectDockNames(String dockName) {
        QueryWrapper<EntityDockList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dock_name",dockName);
        queryWrapper.last(" limit 1");
        return iDaoDockList.selectOne(queryWrapper);
    }
}
